package cn.jr.dao.impl;

import cn.jr.dao.IActiveDao;
import cn.jr.pojo.Active;
import cn.jr.pojo.ShopRun;
import cn.jr.util.DBHelper;
import cn.jr.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ActiveDaoImpl implements IActiveDao {
    DBHelper db=new DBHelper();
    @Override
    public int insertActive(Active active) {
        /**
         * 新增活动
         * @param active 活动对象
         * @return 影响行数
         */
        String sql="insert into active values(?,?,?,?,?)";
        int ActiveId1 =Integer.parseInt(selectActiveEnd().substring(1))+1;
        active.setActiveId("a"+ActiveId1);
        Object[] objs={active.getActiveId(),active.getActiveName(),active.getActiveStartTime(),active.getActiveEndTime(),active.getActiveMoney()};
        return db.dtUpdate(sql,objs);
    }

    @Override
    public int deleteActive(String activeId) {
        /**
         * 删除活动
         * @param activeId 活动主键
         * @return 影响行数
         */
        String sql ="delete from active where activeId=?";
        return db.dtUpdate(sql,activeId);
    }

    @Override
    public int updateActive(Active active) {
        /**
         * 更新活动
         * @param active 活动对象
         * @return 影响行数
         */
        String sql="update active set activeName=?,activeStartTime=?,activeEndTime=?,activeMoney=? where activeId=?";
        Object[] objs={active.getActiveName(),active.getActiveStartTime(),active.getActiveEndTime(),active.getActiveMoney(),active.getActiveId()};
        return db.dtUpdate(sql,objs);
    }



    @Override
    public String selectActiveEnd() {
        /**
         * 查询最后一条数据的活动主键
         * @return 活动主键
         */
        String sql ="select * from active";
        ArrayList list = db.dtQuery(sql, Active.class);
        return ((Active)list.get(list.size()-1)).getActiveId();
    }

    @Override
    public ArrayList<Active> selectByActive() {
        /**
         * 查询所有的活动记录
         * @return 活动记录集合
         */
        String sql ="select * from active";
        return db.dtQuery(sql, Active.class);
    }

    @Override
    public int selectCountByActiveAndPageHelper() {
        /**
         * 查询活动记录的总条数
         * @return 活动记录总条数
         */
        StringBuilder sql =new StringBuilder("select count(*) from active") ;
        Connection con=db.getCon();
        PreparedStatement ps =null;
        ResultSet rs =null;
        try {
            ps = con.prepareStatement(sql.toString());
            rs = ps.executeQuery();
            return rs.next() ? rs.getInt(1) : 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            db.closeAll(con,ps,rs);
        }
    }

    @Override
    public ArrayList<Active> selectByActiveAndPageHelper(Active at, PageHelper ph) {
        /**
         * 查询当前页的活动记录
         * @param at 活动对象
         * @param ph 分页对象
         * @return 活动记录集合
         */
        StringBuilder sql =new StringBuilder("select * from active") ;
        ArrayList list = new ArrayList();
        sql.append(" limit ?,?");
        list.add((ph.getPageIndex()-1)*ph.getShowNumber());
        list.add(ph.getShowNumber());
        return db.dtQuery(sql.toString(),at.getClass(),list.toArray());
    }
}
